/**
 * 
 */
package org.t2.backstage.module.player;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.banish.sql.core.util.QuerySet;
import org.gaming.backstage.PageData;
import org.gaming.service.apidoc.annotation.ApiDocument;
import org.gaming.service.repository.IRepository;
import org.gaming.service.repository.Repository;
import org.gaming.tool.DateTimeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.t2.backstage.module.gameserver.model.GameServer;
import org.t2.backstage.module.gameserver.service.GameServerService;
import org.t2.backstage.module.player.model.CheatLog;
import org.t2.backstage.module.player.model.Player;
import org.t2.backstage.module.player.query.CheatLogQuery;
import org.t2.backstage.module.player.service.PlayerService;
import org.t2.backstage.module.player.vo.CheatLogVo;

import sh.t2.share.apiutil.GameApi;
import sh.t2.share.module.bag.CheatData;
import sh.t2.share.module.bag.CheatTable;

/**
 * @author YY
 */
@Controller
public class CheatLogController {
	
	private static Logger logger = LoggerFactory.getLogger(CheatLogController.class);
	
	@Autowired
	private GameServerService gameServerService;
	@Autowired
	private PlayerService playerService;
	
	@ApiDocument("请求作弊日志数据")
	@RequestMapping(value = "/cheatLog/data.auth")
	@ResponseBody
	public PageData<CheatLogVo> data(CheatLogQuery query) {
		QuerySet querySet = new QuerySet();
		if(query.getServerUids() != null && !query.getServerUids().isEmpty()) {
			Set<Integer> serverIds = new HashSet<>();
			for(long serverUid : query.getServerUids()) {
				GameServer gameServer = gameServerService.getEntity(0, serverUid);
				if(gameServer != null) {
					serverIds.add(gameServer.getServerId());
				}
			}
			querySet.findInSet("server_id", serverIds);
		}
		if(query.getPlayerId() > 0) {
			querySet.addCondition("player_id = ?", query.getPlayerId());
		}
		if(query.getNick() != null && !"".equals(query.getNick())) {
			querySet.addCondition("nick like '%?%'", query.getNick());
		}
		//开始时间
		long queryTime = 0;
		long startTime = query.getStartTime() * 1000L;
		if(startTime > 0) {
			queryTime = startTime;
			String startBorn = DateTimeUtil.formatMillis(DateTimeUtil.YYYY_MM_DD, startTime);
			querySet.addCondition("time >= ?", startBorn);
		} else {
			queryTime = DateTimeUtil.currMillis();
		}
		//结束时间
		long endTime = query.getEndTime() * 1000L;
		if(endTime > 0) {
			String endBorn = DateTimeUtil.formatMillis(DateTimeUtil.YYYY_MM_DD, endTime + DateTimeUtil.ONE_DAY_MILLIS);
			querySet.addCondition("time <= ?", endBorn);
		}
		querySet.limit(query.getPage(), query.getLimit());
		querySet.orderBy("order by id desc");
		querySet.formWhere();
		
		IRepository<CheatLog> logRepository = Repository.get(CheatLog.class);
		List<CheatLog> list = logRepository.getSplitDao().queryListWhere(queryTime, querySet.getWhere(), querySet.getParams());
		
		List<CheatLogVo> resultList = new ArrayList<>();
		for(CheatLog log : list) {
			CheatLogVo vo = new CheatLogVo();
			vo.setId(log.getId());
			vo.setPlayerId(log.getPlayerId());
			vo.setNick(log.getNick());
			vo.setItemId(log.getItemId());
			vo.setItemName(log.getItemName());
			vo.setDelta(log.getDelta());
			vo.setDeltaCheat(log.isDeltaCheat());
			vo.setCurrNum(log.getCurrNum());
			vo.setCurrCheat(log.isCurrCheat());
			vo.setTime(log.getTime());
			vo.setReason(log.getReason());
			
			Player player = playerService.getEntity(log.getPlayerId());
			if(player != null) {
				GameServer gameServer = gameServerService.getByPlatformAndServer(player.getPlatformId(), log.getServerId());
				if(gameServer != null) {
					vo.setServerId(log.getServerId() + "-" + gameServer.getName());
				} else {
					vo.setServerId(log.getServerId() + "");
				}
				vo.setOpenId(player.getOpenId());
				vo.setForbidEndTime(player.getForbidEndTime());
				vo.setForbidRankTime(player.getForbidRankTime());
			} else {
				vo.setServerId(log.getServerId() + "");
			}
			resultList.add(vo);
		}
		
		PageData<CheatLogVo> pageData = new PageData<>();
		pageData.setCount(logRepository.getSplitDao().countWhere(queryTime, querySet.getCountWhere(), querySet.getCountParams()));
		pageData.setData(resultList);
		return pageData;
	}
	
	
	@ApiDocument("上报作弊数据，给游戏服的接口")
	@RequestMapping(value = GameApi.cheatReport)
	@ResponseBody
	public void playerReport(@RequestBody CheatTable cheatTable) {
		List<CheatLog> insertList = new ArrayList<>();
		for(CheatData cheatData : cheatTable.getDatas()) {
			CheatLog log = new CheatLog();
			log.setPlayerId(cheatData.getPlayerId());
			log.setNick(cheatData.getNick());
			log.setItemId(cheatData.getItemId());
			log.setItemName(cheatData.getItemName());
			log.setDelta(cheatData.getDelta());
			log.setDeltaCheat(cheatData.isDeltaCheat());
			log.setCurrNum(cheatData.getCurrNum());
			log.setCurrCheat(cheatData.isCurrCheat());
			log.setTime(cheatData.getTime());
			log.setReason(cheatData.getReason());
			log.setServerId(cheatTable.getServerId());
			insertList.add(log);
		}
		IRepository<CheatLog> logRepository = Repository.get(CheatLog.class);
		logRepository.insertAll(insertList);
		logger.info("收到服务器{}上报的作弊数量{}", cheatTable.getServerId(), insertList.size());
	}
}
